package net.portly.cst;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Manage a map of asset references using the id and the fw_uid (if supplied)
 * Instances can be loaded singularly or as a list.
 * Instances can be located by fw_uid and also by id.
 * 
 * @author cs76
 *
 */
public class StructuralAssetGraph implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Map<String, StructuralAssetInstance> assetGraphByIds = new HashMap<String, StructuralAssetInstance>();
	private Map<String, StructuralAssetInstance> assetGraphByFwUid = new HashMap<String, StructuralAssetInstance>();

	public void put(StructuralAssetInstance instance) {
		if (instance != null) {
			assetGraphByIds.put(instance.getId(), instance);

			if (instance.getFw_uid() != null) {
				assetGraphByFwUid.put(instance.getFw_uid(), instance);
			}
		}
	}
	
	public StructuralAssetInstance getAssetById(String id) {
		return assetGraphByIds.get(id);
	}
	
	public StructuralAssetInstance getAssetByFwUid(String fw_uid) {
		return assetGraphByFwUid.get(fw_uid);
	}

	public void addInstances(List<StructuralAssetInstance> instances) {
		if (instances != null) {
			for (StructuralAssetInstance instance :  instances) {
				assetGraphByIds.put(instance.getId(), instance);
				
				if (instance.getFw_uid() != null) {
					assetGraphByFwUid.put(instance.getFw_uid(), instance);
				}
			}
		}
	}

	public Map<String, StructuralAssetInstance> getAssetGraphByIds() {
		return assetGraphByIds;
	}


}
